package com.bjdd.jdy.ui.signScan;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bjdd.jdy.Constant;
import com.bjdd.jdy.R;
import com.bjdd.jdy.base.BaseActivity;
import com.bjdd.jdy.base.RxManager;
import com.bjdd.jdy.entity.OrderBean;
import com.bjdd.jdy.util.ImageUtil;
import com.bjdd.jdy.util.JumpUtil;
import com.bjdd.jdy.util.L;
import com.bjdd.jdy.util.SpUtil;
import com.bjdd.jdy.util.StatusBarUtil;
import com.bjdd.jdy.util.T;
import com.bjdd.jdy.view.other.CustomDialog;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observable;

public class SignScanActivity extends BaseActivity<SignScanPresenter, SignScanModel> implements SignScanContract.View {


    @Bind(R.id.toolbar_back)
    ImageView toolbarBack;
    @Bind(R.id.title)
    TextView title;
    @Bind(R.id.img_qrcode)
    ImageView imgQrcode;
    String orderNo;
    String orderId;
    Observable observable;
    private RxManager rxManager;

    @Override
    public int getLayoutId() {
        return R.layout.activity_sign_scan;
    }

    @Override
    public void initView() {
        title.setText("扫码签收");
        StatusBarUtil.setColor(this,getResources().getColor(R.color.toolbar_bg),0);
        ImageUtil imageUtil = new ImageUtil(context);
        Bundle bundle = getIntent().getExtras();
        orderNo = bundle.getString("orderNo");
        orderId = bundle.getString("orderId");
        double lat = bundle.getDouble("lat");
        double lng = bundle.getDouble("lng");
        presenter.isCanSign(lat,lng);
        rxManager = new RxManager();
        rxManager.on(Constant.SIGN_ORDER_BACK,msg -> SignBack());
        observable = rxManager.on1(Constant.SIGN_SCAN_SUCCESS,msg -> signScanBack((Integer) msg));
        //String url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQHk8DwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyc1pDLXNVcXJjUzAxMENfZ05wMVEAAgSiOxBZAwSEAwAA";
        //
    }

    @Override
    public void codeBack(String data) {
        imageUtil.loadUrlImage(data,imgQrcode);
    }

    @Override
    public void canNotSign() {
        CustomDialog.Builder builder;
        builder = new CustomDialog.Builder(this);
        mDialog = builder.setMessage("当前不在签收范围")
                .setCancelable(false)
                .setSingleButton("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mDialog.dismiss();
                        finish();
                    }
                })
                .createSingleButtonDialog();
        mDialog.show();
    }

    @Override
    public void canSign() {
        presenter.getQrCode(orderId);
    }

    @OnClick({R.id.tv_code_sign,R.id.toolbar_back})
    public void click(View view){
        switch (view.getId()){
            case R.id.toolbar_back:
                finish();
                break;
            case R.id.tv_code_sign:
                JumpUtil.toSign(context,orderNo);
                break;
        }
    }
    private void SignBack(){
        finish();
    }

    @Override
    protected void onDestroy() {
        rxManager.unregister(Constant.SIGN_SCAN_SUCCESS,observable);
        super.onDestroy();
    }

    private void signScanBack(int type){
        L.d("扫码签收成功，界面跳转");
        JumpUtil.toOrder(context);
        finish();
    };
}
